package com.berry_med.handsetmonitor.volleyFramework;

import android.content.Context;

import com.andware.volley.Request;
import com.andware.volley.RequestQueue;
import com.andware.volley.Response.ErrorListener;
import com.andware.volley.VolleyError;
import com.andware.volley.toolbox.Volley;
import com.berry_med.handsetmonitor.MyEvent.MyVolleyEvent;

import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Created by ishishuji on 14/12/12.
 */
public abstract class MyVolleyBase {

    private static Context context;

    private static String _token;

    private static String _cookie;

    public static int get_position () {
        return _position;
    }

    public static void set_position ( int _position ) {
        MyVolleyBase._position = _position;
    }

    private static int _position;

    public static int getMethod () {
        return _method;
    }

    public static void setMethod ( int method ) {
        _method = method;
    }

    private static int _method;

    private static RequestQueue mRequestQueue;

    public static void init ( Context context ) {
        mRequestQueue = Volley.newRequestQueue ( context );
    }


    public static RequestQueue getRequestQueue () {
        if ( mRequestQueue != null ) {
            return mRequestQueue;
        } else {
            throw new IllegalStateException ( "RequestQueue not initialized" );
        }
    }

    public MyVolleyBase ( Context context ) {
        this.context = context;

    }


    public static void cancelAll ( Context context ) {
        if ( getRequestQueue () != null ) {
            getRequestQueue ().cancelAll ( context );

        }
    }

    public static void cancelByTag ( Object tag ) {
        if ( getRequestQueue () != null ) {
            getRequestQueue ().cancelAll ( tag );
        }
    }

    public static < T > void cancelByAllTag ( List< T > tags ) {
        if ( getRequestQueue () != null ) {
            for ( Object tag : tags ) {
                getRequestQueue ().cancelAll ( tag );
            }
        }
    }

    public static void resumeRequest ( Request request ) {
        if ( getRequestQueue () != null ) {
            getRequestQueue ().add ( request );
        }
    }

    public static void cancelByRequestFilter ( RequestQueue.RequestFilter filter ) {
        if ( getRequestQueue () != null ) {
            getRequestQueue ().cancelAll ( filter );
        }
    }


    protected static < T > void baseHandlerDo ( T obj ) {
        MyVolleyEvent< T > myVolleyEvent = new MyVolleyEvent< T > ();

        myVolleyEvent.set_success ( obj );

        myVolleyEvent.setHasPosition ( false );

        myVolleyEvent.setStatus ( StatusCode.SUCCESS );

        EventBus.getDefault ().post ( myVolleyEvent );
    }

    protected static < T > void positionHandlerDo ( T obj, int position ) {

        MyVolleyEvent< T > myVolleyEvent = new MyVolleyEvent< T > ();

        myVolleyEvent.setPosition ( position );

        myVolleyEvent.setHasPosition ( true );

        myVolleyEvent.set_success ( obj );

        myVolleyEvent.setStatus ( StatusCode.SUCCESS );

        EventBus.getDefault ().post ( myVolleyEvent );
    }


    protected static ErrorListener errorListener = new ErrorListener () {

        @Override
        public void onErrorResponse ( VolleyError error ) {
            MyVolleyEvent< VolleyError > myVolleyEvent = new MyVolleyEvent< VolleyError > ();

            myVolleyEvent.set_error ( error );
            EventBus.getDefault ().post ( myVolleyEvent );
        }
    };


    protected static < T > void baseDeal ( T response ) {

        baseHandlerDo ( response );
    }

    protected static < T > void positionDeal ( T response, int position ) {

        positionHandlerDo ( response, position );

    }

    protected static void addParams ( Request request ) {
        if ( getCookie () != null && ! getCookie ().equals ( "" ) ) {
            request.setCookies ( getCookie () );
        }

        if ( getToken () != null && ! getToken ().equals ( "" ) ) {
            request.setToken ( getToken () );
        }
//        request.setRetryPolicy(new DefaultRetryPolicy (
//                (int) TimeUnit.SECONDS.toMillis(20),0,
//                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    }


    public static String getCookie () {
        return _cookie;
    }

    public static void setCookie ( String cookie ) {
        _cookie = cookie;
    }

    public static String getToken () {
        return _token;
    }

    public static void setToken ( String token ) {
        _token = token;
    }
}
